<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\model\AdminModel;
use app\model\UserModel;

class User extends BaseController
{
    public function query()
    {
        $params = $this->request->param();
        $adminId = $this->request->userInfo->adminId;
        $map = [
            [
                'admin_id',
                '=',
                $adminId
            ]
        ];
        if (!empty($params['username'])) {
            $username = trim($params['username']);
            $map[] = [
                'username',
                'like',
                '%'.$username.'%'
            ];
        }
        if (!empty($params['status'])) {
            $map[] = [
                'status',
                '=',
                $params['status']
            ];
        }


        $list = UserModel::where($map)
            ->order([
                'status' => 'asc'
            ])
            ->paginate([
            'list_rows' => $params['pageSize'],
            'page' => $params['pageNo']
        ])->toArray();
        return show_serve($list);
    }

    public function add()
    {
        $params = $this->request->param();
        $adminId = $this->request->userInfo->adminId;
        $username = trim($params['username']);
        $userModel = UserModel::where('username',$username)->find();
        if ($userModel !== null) {
            return show_serve([],'用户已存在',1);
        }
        UserModel::create([
            'username' => $username,
            'password' => password_hash($params['password'],PASSWORD_DEFAULT),
            'status' => $params['status'] ? 0 : 1,
            'password_origin' => $params['password'],
            'admin_id' => $adminId
        ]);
        return show_serve();
    }

    public function status()
    {
        $params = $this->request->param();
        $userModel = UserModel::find($params['id']);
        $userModel->status = $userModel->status === 0 ? 1: 0;
        $userModel->save();
        return show_serve();

    }

    public function del()
    {
        $params = $this->request->param();
        UserModel::destroy($params['id']);
        return show_serve();
    }

    public function changePassword()
    {
        $params = $this->request->param();
        $userModel = UserModel::find($params['id']);
        if (password_verify($params['password'], $userModel->password) === false) {
            return show_serve(message:'密码错误',code: 1);
        }
        if ($params['new_password'] !== $params['confirm']) {
            return show_serve(message:'两次密码不相同',code: 1);
        }
        $userModel->password = password_hash($params['new_password'],PASSWORD_DEFAULT);
        $userModel->password_origin = $params['new_password'];
        $userModel->save();
        return show_serve();
    }


    public function searchUser()
    {
        $params = $this->request->param();

        $adminId = $this->request->userInfo->adminId;

        $list = UserModel::where([
            ['username', 'like','%'.$params['value'].'%'],
            ['admin_id','=',$adminId]
        ])->field('id,username')
            ->select();

        return show_serve($list);
    }

}